<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  <link rel="stylesheet" title="Default" href="http://acm.math.spbu.ru/~sk1/colorer/my.css">

  <script src="http://acm.math.spbu.ru/~sk1/colorer/highlight.js"></script>
  <script src="http://acm.math.spbu.ru/~sk1/colorer/cpp.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</head>

<body>

<pre><code>
/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com5
 * Date: 2013.10.18
 */

#include &lt;cstdio&gt;
#include &lt;cstring&gt;
#include &lt;cassert&gt;
#include &lt;algorithm&gt;

using namespace std;

#define forn(i, n) for (int i = 0; i &lt; (int)(n); i++)

const int N = 503;

int n, m, a[N][N], b[N][N], row[N][N], col[N][N], f[N][N];

inline void relax( int &a, int b )
{
  a = max(a, b);
}

int main()
{
  while (scanf("%d%d", &n, &m) && (n || m))
  {
    forn(i, n)
      forn(j, m)
        scanf("%d", &a[i][j]), row[i][j + 1] = row[i][j] + a[i][j];
    forn(i, n)
      forn(j, m)
        scanf("%d", &b[i][j]), col[i + 1][j] = col[i][j] + b[i][j];
    memset(f, 0, sizeof(f));
    forn(i, n + 1)
      forn(j, m + 1)
      {
        relax(f[i + 1][j], f[i][j] + row[i][j]);
        relax(f[i][j + 1], f[i][j] + col[i][j]);
      }
    printf("%d\n", f[n][m]);
  }
  return 0;
}

</code></pre>

</body>
</html>

<font style="visibility:hidden">
